var ctx;

function setCtx(ctx) {
	this.ctx = ctx;
	initListener();//添加区域监听事件
}

//验证名称
function checkShopCategoryName(){
	var shopCategoryName = $("#shopCategoryName").val();
	//解决ie9以下placeholder与value重复的问题
	if(shopCategoryName == $("#shopCategoryName").attr("placeholder")){
		shopCategoryName = "";
	}
	
	if(shopCategoryName == null || shopCategoryName == ""){
		layer.tips('名称不能为空!', '#shopCategoryName', {
		    tips: 3,
		    time:1000
		});
		return false;
	}else{
		return true;
	}
}

//等级
function checkCategorylevel(){
	var categorylevel = $("#categorylevel").val();
	if(categorylevel == null || categorylevel == ""){
		layer.tips('请选择等级!', '#categorylevel', {
			tips: 3,
		    time:1000
		});
		return false;
	}else{
		return true;
	}
}

//父级
function checkParent(){
	var parent = $("#parent").val();
	if(parent == null || parent == ""){
			layer.tips('请选择父级分类!', '#parent', {
				tips: 3,
			    time:1000
			});
		return false;
	}else{
		return true;
	}
}
/**
 * 验证楼栋名
 */
function checkBuildingName(){
	var building = $("#buildingid").val();
	if(building == null || building == ""){
		layer.tips('请选择楼栋名!', '#buildingid', {
			tips: 3,
		    time:1000
		});
		return false;
	}else{
		return true;
	}
}

//验证排序不能为空
function checkSort(){
	var reg = /^[0-9]*[1-9][0-9]*$/;
	var sort = $("#sort").val();
	if(sort == null || sort == ""){
		layer.tips('排序不能为空!', '#sort', {
			tips: 3,
			time:1000
		});
		return false;
	}else if(!reg.test(sort)){
		layer.tips('请输入正整数!', '#sort', {
			tips: 3,
			time:1000
		});
		return false;
	}else{
		return true;
	}
}

function initListener(){
	$("#categorylevel").change(function (){
		categorylevel = $("#categorylevel").val();
		if(categorylevel != 1 && categorylevel != ""){
			$("#parantCategory").show();
		}else{
			$("#parantCategory").hide();
			categorylevel = 1;
		}
		$.ajax({
			type : "GET",
			url : ctx + "/shopcategory/getParentBylevel",
			cache : false,
			dataType : 'json',
			data : {
				categorylevel : categorylevel,
			},
			success : function(data) {
				if (data.success) {
					var result = "<option value='" + "'>请选择上级分类</option>";
					$.each(data.obj, function(index, item) {
						result += "<option id='" + item.id + "'" + " value='" + item.id + "'>" + item.name + "</option>";
					});
					$("#parent").html("");
					$("#parent").append(result);
					$("#parent").change();
				}
			}
		});
		
	});
	
	$("#parent").change(function(){
		parentCategory = $("#parent").val();
	});
}
//新增或编辑小区区域(1:新增 2:编辑)
var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
function saveOrUpdateshopcategory(type){
	var id;
	if($("#shopCategoryid").val() != null && $("#shopCategoryid").val() != ''){
		id = $("#shopCategoryid").val();
	}
	var name = $("#shopCategoryName").val();
	var categorylevel = $("#categorylevel").val();
	var describe = $("#describe").val();
	var memo = $("#memo").val();
	var imageLogoUrl = $('#imageLogo').val();
	var sort = $("#sort").val();
	
	if (type == 1) {
		var parentCategory = $("#parent").val();
		if((categorylevel == 2 || categorylevel == 3) && !checkParent() ){
			return;
		}
		if(checkShopCategoryName() && checkCategorylevel() && checkSort()){
			var index1 = layer.load(1);
			$.post(ctx+"/shopcategory/saveOrUpdateshopcategory",
					{
				id:id,
				name:name,
				level:categorylevel,
				parentId:parentCategory,
				describe:describe,
				logo:imageLogoUrl,
				sort:sort,
				memo:memo
					},function(data){
						data = JSON.parse(data);
						if(data.success){
							layer.msg("操作成功",{
								icon: 1,
								time: 2000 //2秒关闭（如果不配置，默认是3秒）
							},function(){
								parent.loadContent("/shopcategory/shopCategoryList");
								parent.layer.close(index);
							});
						} else {
							layer.close(index1);
							layer.msg(data.msg,{
								icon: 2,
								time: 2000 //2秒关闭（如果不配置，默认是3秒）
							});
						}
					});
		}
	}
	if(type == 2){
		$.post(ctx+"/shopcategory/saveOrUpdateshopcategory",
				{
			id:id,
			name:name,
			logo:imageLogoUrl,
			sort:sort,
			describe:describe,
			memo:memo
				},function(data){
					data = JSON.parse(data);
					if(data.success){
						layer.msg("操作成功",{
							icon: 1,
							time: 2000 //2秒关闭（如果不配置，默认是3秒）
						},function(){
							parent.loadContent("/shopcategory/shopCategoryList");
							parent.layer.close(index);
						});
					} else {
						layer.close(index1);
						layer.msg(data.msg,{
							icon: 2,
							time: 2000 //2秒关闭（如果不配置，默认是3秒）
						});
					}
				});
	}
}